package org.example.serviceEdu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.example.ResultCode.R;
import org.example.serviceEdu.entity.EduSubject;
import org.example.serviceEdu.entity.EduTeacher;
import org.example.serviceEdu.entity.excel.SubjectData;
import org.example.serviceEdu.entity.subject.OneSubject;
import org.example.serviceEdu.entity.subject.TwoSubject;
import org.example.serviceEdu.listener.SubjectExcelListener;
import org.example.serviceEdu.mapper.EduSubjectMapper;
import org.example.serviceEdu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author lining
 * @since 2023-10-23
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public  List<OneSubject> getAllOneTwoSubject(){
//        查一级分类
        QueryWrapper<EduSubject> wrapperOne=new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        List<EduSubject> oneSubjectList=baseMapper.selectList(wrapperOne);
//        查二级
        QueryWrapper<EduSubject> wrapperTwo=new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        List<EduSubject> twoSubjectList=baseMapper.selectList(wrapperTwo);
//        封一级
        List<OneSubject> finalSubjectList=new ArrayList<>();
        for (int i=0;i<oneSubjectList.size();i++){
            EduSubject eduSubject=oneSubjectList.get(i);
            OneSubject oneSubject=new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);
//            封二级
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            for(int m=0;m<twoSubjectList.size();m++){
                EduSubject tsubject=twoSubjectList.get(m);
                if (tsubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tsubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }

            }
            oneSubject.setChildren(twoFinalSubjectList);

        }
        return finalSubjectList;



    }
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            InputStream in=file.getInputStream();
            EasyExcel
                    .read(
                            in,
                            SubjectData.class,
                            new SubjectExcelListener(subjectService)
                    )
                    .sheet()
                    .doRead();
        }catch (Exception e){}

    }

}
